//package com.ygl.gateway.filter;
//
//import com.ygl.gateway.util.JwtUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//@Slf4j
//public class AuthFilterFactory extends AbstractNameValueGatewayFilterFactory {
//
//    @Autowired
//    private JwtUtil jwtUtil;
//    private String tokenKey = "token";
//
//    @Override
//    public GatewayFilter apply(NameValueConfig config) {
//        log.info("认证---");
//        return new GatewayFilter() {
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//                ServerHttpRequest request = exchange.getRequest();
//                HttpHeaders headers = request.getHeaders();
//                String token = headers.getFirst(tokenKey);
//                if (token == null) {
//                    token = request.getQueryParams().getFirst(tokenKey);
//                }
//                ServerHttpResponse response = exchange.getResponse();
//                Boolean valid = jwtUtil.validateToken(token);
//                if (!valid) {
//                    response.setStatusCode(HttpStatus.UNAUTHORIZED);
//                    return response.setComplete();
//                }
//
//                return chain.filter(exchange);
//            }
//        };
//    }
//}
